﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ReporterEngine
{
    public static class PathParser
    {
        public static string Parse(string path)
        {
            var result = "source" + path + ">";

            result = result.Replace("\"", "'");

            //replace only methods , because later is all method converted
            result = getFunctionsExp.Replace(result, ".${method}(\"${params}\")");
            result = getFunctions2Exp.Replace(result, ".${method}(\"${params}\")");

            result = getTypesExp.Replace(result, ".GetTypes(\"${types}\")");

            result = getPropertiesExp.Replace(result, ".GetProperties(\"${properties}\")");

            result = result.Replace("[", ".filter(lambda x:x");
            result = result.Replace("]", ")");

            //remove separators
            result = separatorExp.Replace(result, "");
            
            return result;
        }
        #region regex
        //> (Use Cases)  > (Use Cases , "events" ) > property1, prop2 > .members() < ref >prop4,prop5> [ (Events) > prop5 > prop6 > count() == 7] > propval
        public static readonly Regex getTypesExp = new Regex("(?<=[\\>\\[])(?<!\\w)[\\s]*\\((?<types>[\\w\\s\\,\\\"\\']*)\\)");
        //[<>]+(?<properties>[\s]*[\w\,]+[\w\s\,]*)
        public static readonly Regex getPropertiesExp = new Regex("(?<=[\\<\\>\\[])[\\s]*(?<properties>[\\w]+[\\w\\s\\,\\']*)[\\s]*(?=[$\\>\\<])");

        public static readonly Regex getFunctionsExp = new Regex("(?<=[\\>\\[])[\\s\\.]*(?<method>[\\w\\.]+)\\((?<params>[\\w\\s\\,\\\"\\']*)\\)");
        //without comma
        public static readonly Regex getFunctions2Exp = new Regex("(?<=[\\>\\[])[\\s]*(?<method>[\\w]+)\\((?<params>[\\w\\s\\,\\\"\\']*)\\)");

        public static readonly Regex separatorExp = new Regex("[\\s]*[\\>\\<][\\s]*");

        //public static readonly Regex typesExp = new Regex("\\>[\\s]*\\(");

        #endregion
    }
}
